package com.dmatek.uwb.xml.container;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.dmatek.uwb.util.tool.StringUtil;
/***
 * 解析配置文件的抽象类
 * @author zhangfu
 * @data 2019年6月22日 下午3:08:57
 * @Description
 */
public abstract class BaseXmlContainer implements IXmlParse {
	private Map<String, Object> xmlContainer;
	public BaseXmlContainer() {
		super();
		this.xmlContainer = new HashMap<String, Object>();
	}
	/***
	 * 读取根元素
	 * @return
	 * @throws DocumentException
	 */
	protected Element obtainRoot(String xmlPath) throws DocumentException {
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(xmlPath);
		return document.getRootElement();
	}
 	/***
 	 * 从根元素获取maps元素
 	 * @param root
 	 * @return
 	 */
	protected List<Element> obtainMapElements(Element root) {
		String clazzName = "";
		List<Element> mapElements = new ArrayList<Element>();
		List<Element> elements = root.elements("bean");
		for(Element ele:elements) {
			clazzName = ele.attributeValue("class");
			if(!StringUtil.isEmpty(clazzName) && "map".equalsIgnoreCase(clazzName)) {
				mapElements.add(ele);
			}
		}
		return mapElements;
	}
 	/***
 	 * 获取list类型的元素
 	 * @param root
 	 * @return
 	 */
 	protected List<Element> obtainListElements(Element root) {
 		String clazzName = "";
 		List<Element> listElements = new ArrayList<Element>();
 		List<Element> elements = root.elements("bean");
		for(Element ele:elements) {
			clazzName = ele.attributeValue("class");
			if(!StringUtil.isEmpty(clazzName) && "list".equalsIgnoreCase(clazzName)) {
				listElements.add(ele);
			}
		}
		return listElements;
 	}
 	/***
 	 * 获取基础类元素
 	 * @param root
 	 * @return
 	 */
 	protected List<Element> obtainDomElements(Element root) {
		String clazzName = "";
		List<Element> domElements = new ArrayList<Element>();
		List<Element> elements = root.elements("bean");
		for(Element ele:elements) {
			clazzName = ele.attributeValue("class");
			if(!StringUtil.isEmpty(clazzName) && !"map".equalsIgnoreCase(clazzName)) {
				domElements.add(ele);
			}
		}
		return domElements;
	}
	public Map<String, Object> getXmlContainer() {
		return xmlContainer;
	}
	public void setXmlContainer(Map<String, Object> xmlContainer) {
		this.xmlContainer = xmlContainer;
	}
}
